package cn.shyouchi.wuguanjia.android.ui.event.checknum;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AddCheckNumDesActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.edit_check_num_des)
    EditText mEditCheckNumDes;

    EventApi mEventApi;
    String projectid;


    List<String> mDiffList = new ArrayList<>();
    @BindView(R.id.tv_report_time_dif_value)
    TextView mTvReportTimeDifValue;


    CheckNumLineEntity.ResultMessageBean model;

    boolean isEdit;

    @Override
    public int initContentView() {
        return R.layout.activity_add_check_num_des;
    }

    @Override
    public void initInjector() {
        mEventApi = new EventApi(mContext);

        model = getIntent().getParcelableExtra("line");

        projectid = getIntent().getStringExtra("projectid");
        mDiffList.add("5分钟");
        mDiffList.add("10分钟");
        mDiffList.add("15分钟");
        mDiffList.add("20分钟");
        mDiffList.add("25分钟");
        mDiffList.add("30分钟");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        if (model != null) {
            isEdit = true;
            mEditCheckNumDes.setText(model.getName());
            mTvReportTimeDifValue.setText(model.getDifference());
        }


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add("保存");
        menuItem.setShowAsAction(2);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals("保存")) {
            generateEntityExit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void generateEntityExit() {
        if (isEdit) {
            mEventApi.upCheckNumLine(projectid, String.valueOf(model.getId()), mEditCheckNumDes.getText().toString(), mTvReportTimeDifValue.getText().toString())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onNext(BaseRequestResult result) {
                            EventBus.getDefault().post(new DataChangeEvent());
                            finish();
                        }
                    });
        } else {
            mEventApi.addCheckNumLine(projectid, mEditCheckNumDes.getText().toString(), mTvReportTimeDifValue.getText().toString())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onNext(BaseRequestResult result) {
                            EventBus.getDefault().post(new DataChangeEvent());
                            finish();
                        }
                    });
        }


    }


    private synchronized void showSelDialog(String title, List<String> sels) {
        final String[] choices = sels.toArray(new String[sels.size()]);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT);
        TextView tvTitle = (TextView) getLayoutInflater().inflate(R.layout.dialog_title, null);
        tvTitle.setText(title);
        builder.setCustomTitle(tvTitle);
        builder.setItems(choices, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mTvReportTimeDifValue.setText(choices[which]);
            }


        });
        builder.setNegativeButton("取消", null);
        builder.create().show();
    }


    @OnClick(R.id.tv_report_time_dif_value)
    public void onClick() {

        showSelDialog("选择巡检误差时间", mDiffList);

    }

}
